package com.iotek.ssm.interceptor;

import com.iotek.ssm.pojo.Manager;
import com.iotek.ssm.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AdminLoginInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 得到请求的url
        String url = request.getRequestURL().toString();
        System.out.println("*************URL = " + url + "**********AdminLoginInterceptor");
        // 判断是否公开
//        if (url.contains("login") || url.contains("images") || url.contains("css") || url.contains("script") || url.contains("register"))
//            return true;

        HttpSession session = request.getSession();
        Manager manager = (Manager) session.getAttribute("manager");
        //判断用户信息是否存在
        if (manager != null)
            return true;
        //跳转登录页面
        request.getRequestDispatcher("../admin/login.jsp").forward(request, response);
        return false;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }
}
